package com.knowledge.knowledge_notes.service.impl;

import com.knowledge.knowledge_notes.dao.AdminRolePermissionDAO;
import com.knowledge.knowledge_notes.pojo.AdminPermission;
import com.knowledge.knowledge_notes.pojo.AdminRolePermission;
import com.knowledge.knowledge_notes.service.AdminRolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdminRolePermissionServiceImpl implements AdminRolePermissionService {

    @Autowired
    AdminRolePermissionDAO adminRolePermissionDAO;


    @Override
    public List<AdminRolePermission> findAllByRid(int rid) {
        return adminRolePermissionDAO.findAllByRid(rid);
    }

    @Override
    @Transactional
    public void savePermChanges(int rid, List<AdminPermission> perms) {
        adminRolePermissionDAO.deleteAllByRid(rid);
        List<AdminRolePermission> rps = new ArrayList<>();
        perms.stream().forEach(p -> {
            AdminRolePermission rp = new AdminRolePermission();
            rp.setRid(rid);
            rp.setPid(p.getId());
            rps.add(rp);
        });
        adminRolePermissionDAO.saveAll(rps);
    }
    public static void main(String[] args) {
        List rps = new ArrayList();
        rps.add(new Object());
//        rps.stream().forEach(e -> {
        rps.forEach(e -> {
            System.out.println(1);
        });
    }
}
